package cn.gdpu.course.web.controller.admin;

import cn.gdpu.course.constant.Constant;
import cn.gdpu.course.dao.CourseDao;
import cn.gdpu.course.dao.CourseTypeDao;
import cn.gdpu.course.dao.TeacherDao;
import cn.gdpu.course.dao.domain.Course;
import cn.gdpu.course.dao.domain.CourseType;
import cn.gdpu.course.dao.domain.Teacher;
import cn.gdpu.course.web.bean.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2018/3/14.
 */

@Controller
@RequestMapping("/admin/course")
public class AdmCourseController {

	@Resource
	private CourseDao courseDao;

	@Resource
	private CourseTypeDao courseTypeDao;

	@Resource
	private TeacherDao teacherDao;

	@RequestMapping(method = RequestMethod.GET)
	public String index(HttpServletRequest request, ModelMap model) {
		String username = (String) request.getSession().getAttribute(Constant.user_name);
		List<CourseType> courseTypes = courseTypeDao.find(null);
		List<Teacher> teachers = teacherDao.find(null);
		model.addAttribute("username", username);
		model.addAttribute("courseTypes", courseTypes);
		model.addAttribute("teachers",teachers);
		return "admin/course";
	}

	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
	public PageBean<Course> list(ModelMap model, Course course) {
		List<Course> courses = courseDao.find(course);
		PageBean<Course> data = new PageBean<Course>();
		data.setData(courses);
		return data;
	}

	@ResponseBody
	@RequestMapping(value = "/findById", method = {RequestMethod.GET, RequestMethod.POST})
	public Course findById(ModelMap model, Course course) {
		return courseDao.findById(course);
	}

	@ResponseBody
	@RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST})
	public int save(ModelMap model, Course course) {
		Course b = courseDao.findByBno(course);
		if (b != null) {
			return -1;
		}
		/**
		 * 判断上课时间是否有冲突
		 */
		Course c = new Course();
		c.setTeacher(course.getTeacher());
		List<Course> courses = courseDao.find(c);
		for (int i = 0; i < courses.size(); i++) {
			if (courses.get(i).getCtime().equals(course.getCtime()))
				return -2;
		}
		return courseDao.save(course) ? 1 : 0;
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public Boolean update(ModelMap model, Course course) {
		Course b = courseDao.findById(course);
		course.setTotal(b.getTotal() + (course.getRemain() - b.getRemain()));
		return courseDao.update(course);
	}

	@ResponseBody
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public Boolean delete(ModelMap model, Course course) {
		course = courseDao.findById(course);
		if (course.getTotal()-course.getRemain() > 0)
			return false;
		return courseDao.delete(course);
	}

}
